package com.freesky.biz.activity.controller;

import com.freesky.biz.activity.domain.TActivityConfig;
import com.freesky.biz.activity.domain.TActivityConfigRules;
import com.freesky.biz.activity.service.IActivityConfigService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/activity/config")
public class ActivityConfigController extends BaseController {

    @Autowired
    private IActivityConfigService activityConfigService;

    /**
     * 获取活动配置列表
     */
    @PreAuthorize("@ss.hasPermi('activity:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(TActivityConfig config) {
        startPage();
        List<TActivityConfig> list = activityConfigService.findList(config);
        return getDataTable(list);
    }

    /**
     * 根据ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('activity:config:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable String configId) {
        TActivityConfig activityConfig = activityConfigService.get(configId);
        List<TActivityConfigRules> activityRulesModelList = activityConfigService.getActivityRulesByActivityId(configId);
        if(activityRulesModelList!=null) {
            activityConfig.setTActivityConfigRulesList(activityRulesModelList);
        }
        return success(activityConfig);
    }

    /**
     * 新增活动配置
     */
    @PreAuthorize("@ss.hasPermi('activity:config:add')")
    @Log(title = "活动配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TActivityConfig config) {
        config.setId(SeqUtil.getSeqId());
        config.setCreateBy(String.valueOf(getUserId()));
        return toAjax(activityConfigService.insert(config));
    }

    /**
     * 修改活动配置
     */
    @PreAuthorize("@ss.hasPermi('activity:config:edit')")
    @Log(title = "活动配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TActivityConfig config) {
        config.setUpdateBy(String.valueOf(getUserId()));
        return toAjax(activityConfigService.update(config));
    }


    /**
     * 删除活动配置
     */
    @PreAuthorize("@ss.hasPermi('activity:config:remove')")
    @Log(title = "活动配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable String[] configIds) {
        activityConfigService.deleteByIds(configIds);
        return success();
    }

    /**
     * 根据ID获取活动规则
     */
    @PreAuthorize("@ss.hasPermi('activity:config:query')")
    @GetMapping("/getActivityRulesById")
    public AjaxResult getActivityRulesById(@NotNull String id) {
        TActivityConfigRules activityRulesModel = activityConfigService.getActivityRulesById(id);
        return success(activityRulesModel);
    }

    /**
     * 新增活动规则配置
     */
    @PreAuthorize("@ss.hasPermi('activity:config:add')")
    @Log(title = "活动规则配置", businessType = BusinessType.INSERT)
    @PostMapping("/activityRules")
    public AjaxResult addActivityRules(@Validated @RequestBody TActivityConfigRules configRules) {
        configRules.setId(SeqUtil.getSeqId());
        return toAjax(activityConfigService.addActivityRules(configRules));
    }

    /**
     * 新增活动规则配置
     */
    @PreAuthorize("@ss.hasPermi('activity:config:add')")
    @Log(title = "活动规则配置", businessType = BusinessType.UPDATE)
    @PutMapping("/activityRules")
    public AjaxResult modifyActivityRules(@Validated @RequestBody TActivityConfigRules configRules) {
        return toAjax(activityConfigService.updateActivityRules(configRules));
    }

    /**
     * 删除活动规则配置
     */
    @PreAuthorize("@ss.hasPermi('activity:config:remove')")
    @Log(title = "活动规则配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/activityRules/{configId}")
    public AjaxResult remove(@PathVariable String configId) {
        activityConfigService.deleteActivityRulesById(configId);
        return success();
    }
}
